﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using AdminSite.Models;

namespace AdminSite.Services
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "CurrencyWCFService" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select CurrencyWCFService.svc or CurrencyWCFService.svc.cs at the Solution Explorer and start debugging.
    public class CurrencyWCFService : ICurrencyWCFService
    {
        private CurrencyManagerAdminDBEntities _db = new CurrencyManagerAdminDBEntities();

        #region Implementation of ICurrencyWCFService

        public DataTable GetCompaniesList()
        {
            DataTable dt = new DataTable("Companies");
            DataColumn col;
            col = new DataColumn("CompanyID");
            col.DataType = Type.GetType("System.Int32");
            dt.Columns.Add(col);
            col = new DataColumn("CompanyName");
            col.DataType = Type.GetType("System.String");
            dt.Columns.Add(col);
            foreach (Company company in _db.Companies)
            {
                dt.Rows.Add(company.CompanyId, company.CompanyName);
            }
            return dt;
        }

        public DataTable GetCurrencyForCompany(string companyName)
        {
            DataTable dt = new DataTable("Currencies");
            DataColumn col;
            col = new DataColumn("CurrencyName");
            col.DataType = Type.GetType("System.String");
            dt.Columns.Add(col);
            col = new DataColumn("BankBuy");
            col.DataType = Type.GetType("System.Decimal");
            dt.Columns.Add(col);
            col = new DataColumn("BankSell");
            col.DataType = Type.GetType("System.Decimal");
            dt.Columns.Add(col);
            int companyId = (from c in _db.Companies
                             where c.CompanyName == companyName
                             select c.CompanyId).SingleOrDefault();
            foreach (Currency currency in _db.Currencies.Where(c => c.CompanyId == companyId))
            {
                dt.Rows.Add(currency.CurrencyName, currency.BankBuy, currency.BankSell);
            }
            return dt;
        }

        #endregion
    }
}
